<?php 
	class Timus extends CI_Model{

	 	function __construct(){
        	parent::__construct();
    	}

    	function get_timu($limit = 0, $offset = 1, $conditions = array()){
    		$where = "1 = 1";
			if(!empty($conditions['chapter_id'])){
				$where .= " AND tm.book_chapter_id = '{$conditions['chapter_id']}'";
			}
			if(!empty($conditions['timu_id'])){
				$where .= " AND tm.id = '{$conditions['timu_children_id']}'";
			}
			$sql = "SELECT SQL_CALC_FOUND_ROWS tm.* FROM {$this->db->dbprefix}tk_timu_main AS tm ";
			$sql .= "LEFT JOIN {$this->db->dbprefix}tk_timu_historys AS th ON(th.timu_id=tm.id AND th.volume_id=0 AND th.volume_history_id=0 AND th.uid='{$this->user_data['id']}' ) ";
			$sql .= "WHERE $where /*GROUP BY tm.id*/ ORDER BY tm.id DESC LIMIT $limit, $offset";
			$result = $this->db->query($sql)->result_array();
			$row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
			return array("count" => $row_num['row_num'], "result" => $result);
    	}

    	function get_timu_children($timu_id){
    		$sql = "SELECT * FROM {$this->db->dbprefix}tk_timu_children WHERE timu_id='{$timu_id}'";
    		return $this->db->query($sql)->result_array();
    	}

    	function get_timu_reuslt($timu_children_id){
    		$sql = "SELECT * FROM {$this->db->dbprefix}tk_timu_children_result WHERE timu_children_id = $timu_children_id ORDER BY id ASC";
			$result = $this->db->query($sql)->result_array();
			return $result;
    	}
	}